package com.eqdd.yiqidian.rongyun.plugin;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.bean.rebuild.ShareEnterConf;
import com.eqdd.yiqidian.bean.rebuild.UserCardInfoResultBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.rongyun.message.mingpian.Content;
import com.eqdd.yiqidian.rongyun.message.mingpian.MingpianMessage;
import com.eqdd.yiqidian.ui.share.ShareEnterActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

import io.rong.imkit.RongExtension;
import io.rong.imkit.RongIM;
import io.rong.imkit.plugin.IPluginModule;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

/**
 * Created by lvzhihao on 17-4-30.
 */

public class MingpianPluginModel implements IPluginModule {
    Conversation.ConversationType conversationType;
    String targetId;

    @Override
    public Drawable obtainDrawable(Context context) {
        //设置插件 Plugin 图标
        return ContextCompat.getDrawable(context, R.drawable.mingpian_selector);
    }

    @Override
    public String obtainTitle(Context context) {
        //设置插件 Plugin 展示文字
        return "名片";
    }

    @Override
    public void onClick(final Fragment currentFragment, RongExtension extension) {
        //示例获取 会话类型、targetId、Context,此处可根据产品需求自定义逻辑，如:开启新的 Activity 等。
        conversationType = extension.getConversationType();
        targetId = extension.getTargetId();
        Intent intent = new Intent(currentFragment.getActivity(), ShareEnterActivity.class);
        ShareConf shareConf = new ShareConf().setTitle("请选择")
                .setRightTitle("发送")
                .setMaxNum(5)
                .setMaxTip("最多%d人");

        ShareEnterConf shareEnterConf = (ShareEnterConf) new ShareEnterConf()
                .setFriendConf(shareConf)
                .setQunChooseConf(shareConf)
                .setQunListConf(shareConf)
                .setQunMemberConf(shareConf)
                .setStaffConf(shareConf)
                .setShare(false)
                .setAll(false)
                .setTitle("请选择")
                .setMaxNum(5)
                .setRightTitle("发送")
                .setMaxTip("最多%d人");
        intent.putExtra("bean", shareEnterConf);
        extension.startActivityForPluginResult(intent, Config.SHARE_ENTER, this);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Config.SHARE_ENTER) {
            if (resultCode == Config.SUCCESS) {
                ArrayList<SelectBean> selectBeans = data.getParcelableArrayListExtra(Config.SELECTED_BEANS);
                for (SelectBean selectBean : selectBeans) {
                    if (selectBean.getType() == Conversation.ConversationType.PRIVATE) {
                        OkGo.<HttpResult<UserCardInfoResultBean>>post(HttpConfig.BASE_URL + HttpConfig.USER_CARD_INFO)
                                .params("userGuid", selectBean.getGuid())
                                .execute(new JsonCallBack<HttpResult<UserCardInfoResultBean>>() {
                                    @Override
                                    public void onSuccess(Response<HttpResult<UserCardInfoResultBean>> response) {
                                        HttpResult<UserCardInfoResultBean> httpResult = response.body();
                                        ToastUtil.showShort(httpResult.getMsg());
                                        if (httpResult.getStatus() == 200) {
                                            UserCardInfoResultBean items = httpResult.getItems();
                                            RongIM.getInstance().refreshUserInfoCache(new UserInfo(items.getUserGuid(), items.getUpname(), Uri.parse(items.getPhoto())));
                                            Content content = null;
                                            if (items != null) {
                                                content = new Content(items.getUname(), items.getPhoto(),
                                                        items.getDepartment(), items.getPost(),
                                                        items.getCompany(), items.getUserGuid(), "0");
                                            } else {
                                                content = new Content(selectBean.getContent(), selectBean.getHeadImg() == null ? "0" : selectBean.getHeadImg(),
                                                        "0", "0", "0", selectBean.getGuid(), "0");
                                            }
                                            MingpianMessage obtain = MingpianMessage.obtain(content);
                                            Message message = Message.obtain(targetId, conversationType, obtain);
                                            RongIM.getInstance().sendMessage(message, null, null, new IRongCallback.ISendMessageCallback() {
                                                @Override
                                                public void onAttached(Message message) {

                                                }

                                                @Override
                                                public void onSuccess(Message message) {
                                                    System.out.println("发送成功");
                                                }

                                                @Override
                                                public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                                    System.out.println(errorCode.toString());
                                                }

                                            });
                                        }
                                    }
                                });
                    }
                }
            }
        }
    }
}
